package org.webautotest.reservation;




import org.openqa.selenium.By;

import cn.bmyi.webautotest.aimee.BrowserEmulator;
import cn.bmyi.webautotest.aimee.BrowserEmulator.Element;
import cn.bmyi.webautotest.common.Config;

/**
 * 封装填写信息页面操作及页面元素
 * @author gaowen
 *
 */
public class FillMessagePage
{
	public BrowserEmulator browser;
	public static class PageElement
	{
		public static final String SEARCH_TITLE_XPATH="/html/body/div/header/div[1]";//标题栏
		public static final String SEARCH_LOGO_CLASS="header-middle-logo";//瑞慈体检logo
		public static final String SEARCH_NNAMETXT_ID="txt_name";//姓名输入框
		public static final String SEARCH_ISMARRYTXT_ID="txt_isMarry";//婚姻状况选择框
		public static final String SEARCH_CERTNOTXT_ID="txt_certNo";//卡号输入框
		public static final String SEARCH_MOBLILE_ID="txt_mobile";//手机号输入框
		public static final String SEARCH_EMAIL_ID="txt_email";//邮箱输入框
		public static final String SEARCH_NEXTBUTTON_XPath="/html/body/div/div[1]/a";//下一步按钮
		public static final String SEARCH_MARRIED_XPATH="/html/body/div/div[1]/div/div/span[1]";//已婚选项
		public static final String SEARCH_UNMARRIED_XPATH="/html/body/div/div[1]/div/div/span[2]";//未婚选项
		public static final String SEARCH_MYSECRET_XPATH="/html/body/div/div[1]/div/div/span[3]";//保密选项
		public static final String SEARCH_POPBOX_XPATH="/html/body/div[3]";//报错弹窗
		public static final String SEARCH_POPTITLE_XPATH="/html/body/div[3]/div[1]";//报错弹窗标题
		public static final String SEARCH_POPCONTENT_XPATH="/html/body/div[3]/div[2]";//报错弹窗提示内容
		public static final String SEARCH_POPBUTTON_XPATH="/html/body/div[3]/div[3]/a";//报错弹窗确认按钮
	}
	
	public FillMessagePage(BrowserEmulator browser)
	{
		this.browser=browser;
		
	}
	
	/**
	 * 获取标题
	 * @return 标题
	 */
	public String getTitle()
	{
		return browser.findElement("xpath", PageElement.SEARCH_TITLE_XPATH).getText();
	}
	
	/**
	 * 判断Logo是否显示
	 * @return True:显示；False：未显示
	 */
	public boolean isLogeDisPlay()
	{
		try 
		{
			browser.findElement("class", PageElement.SEARCH_LOGO_CLASS);
			return true;
		}
		catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 等待Logo消失
	 */
	public void waiteLogoDespear()
	{
		By locator =By.className(PageElement.SEARCH_LOGO_CLASS);
		Element logo=browser.new Element(locator);
		logo.waitUntilConceal();
	}
	
	/**
	 * 输入姓名
	 * @param Name 姓名
	 */
	public void inputName(CharSequence...Name)
	{
		browser.findElement("id", PageElement.SEARCH_NNAMETXT_ID).clear();
		browser.findElement("id", PageElement.SEARCH_NNAMETXT_ID).type(Name);
	}
			
	/**
	 * 输入身份证号
	 * @param CertNo 身份证号
	 */
	public void inputCertNo(CharSequence...CertNo)
	{
		browser.findElement("id", PageElement.SEARCH_CERTNOTXT_ID).clear();
		browser.findElement("id", PageElement.SEARCH_CERTNOTXT_ID).type(CertNo);
	}
	
	/**
	 * 选择婚姻状况
	 * @param isMarry married：选择已婚；unmarried：选择未婚；mysecret：选择保密
	 */
	public void chooseIsMarry(String isMarry)
	{
		browser.findElement("id", PageElement.SEARCH_ISMARRYTXT_ID).click();
		switch (isMarry)
		{
			case "married":
				By locator=By.xpath(PageElement.SEARCH_MARRIED_XPATH);
				Element married=browser.new Element(locator);
				married.waitUntilDisplay();
				browser.findElement("xpath", PageElement.SEARCH_MARRIED_XPATH).click();
				break;
			case "unmarried":
				locator=By.xpath(PageElement.SEARCH_UNMARRIED_XPATH);
				married=browser.new Element(locator);
				married.waitUntilDisplay();
				browser.findElement("xpath", PageElement.SEARCH_UNMARRIED_XPATH).click();
				break;
			case "mysecret":
				locator=By.xpath(PageElement.SEARCH_MYSECRET_XPATH);
				married=browser.new Element(locator);
				married.waitUntilDisplay();
				browser.findElement("xpath", PageElement.SEARCH_MYSECRET_XPATH).click();
			default:
				break;
		}
	}
	
	/**
	 * 等待婚姻下拉框消失
	 */
	public void waitChoiseBoxDespear()
	{
		By locator =By.xpath(PageElement.SEARCH_MARRIED_XPATH);
		Element choiseBox=browser.new Element(locator);
		choiseBox.waitUntilConceal();
	}
	
	/**
	 * 输入手机号
	 * @param Mobile 手机号
	 */
	public void inputMobile(CharSequence...Mobile)
	{
		browser.findElement("id", PageElement.SEARCH_MOBLILE_ID).clear();
		browser.findElement("id", PageElement.SEARCH_MOBLILE_ID).type(Mobile);
	}
	
	
	/**
	 * 输入邮箱
	 * @param Email 邮箱地址
	 */
	public void inputEmail(CharSequence...Email)
	{
		browser.findElement("id", PageElement.SEARCH_EMAIL_ID).clear();
		browser.findElement("id", PageElement.SEARCH_EMAIL_ID).type(Email);
	}
	
	
	/**
	 * 点击下一步按钮
	 */
	public void clickNextButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_NEXTBUTTON_XPath).click();
	}
	
	/**
	 * 等待下一步按钮消失
	 */
	public void waitNextButtonDespear()
	{
		By locator=By.xpath(PageElement.SEARCH_NEXTBUTTON_XPath);
		Element nextButton=browser.new Element(locator);
		nextButton.waitUntilConceal();
	}
	
	/**
	 * 获取下一步按钮属性
	 * @return 按钮class属性
	 */
	public String getButtonAttribute()
	{
		return browser.findElement("xpath", PageElement.SEARCH_NEXTBUTTON_XPath).getAttribute("class");
	}	
	
	/**
	 * 检查是否弹窗
	 * @return true:弹窗了；false：没弹窗
	 */
	public boolean isPopbox()
	{
		try
		{
			browser.findElement("xpath", PageElement.SEARCH_POPBOX_XPATH);
			return true;
		}
		catch (Exception e)
		{
			return false;
		}
	}
	
	/**
	 * 获取弹窗标题
	 * @return 弹窗标题
	 */
	public String getPopTitle()
	{
		return browser.findElement("xpath", PageElement.SEARCH_POPTITLE_XPATH).getText();
				
	}
	
	/**
	 * 获取弹窗内容
	 * @return 弹窗内容
	 */
	public String getPopContent()
	{
		return browser.findElement("xpath", PageElement.SEARCH_POPCONTENT_XPATH).getText();
	}
	
	/**
	 * 点击弹窗确认按钮
	 */
	public void clickPopButton()
	{
		browser.findElement("xpath", PageElement.SEARCH_POPBUTTON_XPATH).click();
	}
	
	/**
	 * 填写信息点击下一步按钮
	 * @param name 姓名
	 * @param isMarry 是否已婚
	 * @param certNo 身份证号
	 * @param mobile 手机号
	 * @param email 邮箱地址
	 */
	public void fishFillMessage()
	{
		Config.open(RigistCardPage.class.getResourceAsStream("/Config.txt"));
		String name= Config.getProperty("name");
		String isMarry= Config.getProperty("isMarry");
		String certNo= Config.getProperty("certNo");
		String mobile= Config.getProperty("mobile");
		
		this.inputName(name);
		this.chooseIsMarry(isMarry);
		this.inputCertNo(certNo);
		this.inputMobile(mobile);
		this.inputCertNo(certNo);
		if (getButtonAttribute().equals("order appbtn disabled"))
		{
			this.inputCertNo(certNo);
		}
		this.clickNextButton();
		this.waitNextButtonDespear();
	}
}
